﻿using GalaSoft.MvvmLight.CommandWpf;
using HuiZe.education.Common;
using HuiZe.education.Model;
using HuiZe.education.View;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Forms;

namespace HuiZe.education.ViewModel
{
    public class MySpaceViewModel : ViewModelBase
    {
        public MySpaceView mySpaceView { get; set; }
        public GroupView groupView { get; set; }
        public GroupModel SelectedGroups { get; set; }
        public GroupModel SelectedFileGroup { get; set; }
        public GroupModel SelectedUploadGroup { get; set; }
        public string NewGroupName { get; set; }
        public int EditGroupId { get; set; }
        public int useProgress { get; set; }
        public double useProgress2 { get; set; }
        public string serachName { get; set; }

        public int pageCount { get; set; }

        public int pageCount2 { get; set; }

        public int current_upload_id { get; set; }
        public string current_attachment_id { get; set; }

        double _size_used;
        public double size_used
        {
            get { return _size_used; }
            set
            {
                _size_used = value;
                RaisePropertyChanged("size_used");
            }
        }

        double _size_total;
        public double size_total
        {
            get { return _size_total; }
            set
            {
                _size_total = value;
                RaisePropertyChanged("size_total");
            }
        }

        long _upload_size_total;
        public long upload_size_total
        {
            get { return _upload_size_total; }
            set
            {
                _upload_size_total = value;
                RaisePropertyChanged("upload_size_total");
            }
        }

        long _upload_size_value;
        public long upload_size_value
        {
            get { return _upload_size_value; }
            set
            {
                _upload_size_value = value;
                RaisePropertyChanged("upload_size_value");
            }
        }

        string _upload_file_name;
        public string upload_file_name
        {
            get { return _upload_file_name; }
            set
            {
                _upload_file_name = value;
                RaisePropertyChanged("upload_file_name");
            }
        }

        Visibility _v_upload_mask;
        public Visibility v_upload_mask
        {
            get { return _v_upload_mask; }
            set
            {
                _v_upload_mask = value;
                RaisePropertyChanged("v_upload_mask");
            }
        }

        public int SeletedResCount { get; set; }
        public RelayCommand<string> UpLoadComand
        {
            get
            {
                return new RelayCommand<string>(new Action<string>((s) =>
                {
                    v_upload_mask = Visibility.Collapsed;
                    UploadView uploadView = new UploadView();
                    uploadView.Owner = System.Windows.Application.Current.MainWindow;
                    uploadView.DataContext = this;
                    if (uploadView.ShowDialog() == true)
                    {
                        mySpaceView.Refresh();
                    }
                }));
            }
        }

        public RelayCommand GroupCommand
        {
            get
            {
                return new RelayCommand(new Action(() =>
                {
                    GroupView groupView = new GroupView() { DataContext = this };
                    groupView.ShowDialog();
                    SelectedGroups = MyGroups_Filter[0];

                }));
            }
        }

        public RelayCommand NewGroupCommand
        {
            get
            {
                return new RelayCommand(new Action(() =>
                    {
                        NewGroupName = string.Empty;
                        NewGroupView newGroupView = new NewGroupView() { DataContext = this };
                        newGroupView.ShowDialog();
                        if (newGroupView.ShowDialogResult)
                        {
                            MyGroups.Add(new GroupModel() { GroupName = NewGroupName });
                            NewGroupName = "";
                        }
                    }));
            }
        }
        public RelayCommand EditGroupCommand
        {
            get
            {
                return new RelayCommand(new Action(() =>
                {
                    EditGroupView editGroupView = new EditGroupView() { DataContext = this };
                    editGroupView.groupId = EditGroupId;
                    editGroupView.ShowDialog();
                    if (editGroupView.ShowDialogResult)
                    {

                    }
                }));
            }
        }

        public ObservableCollection<GroupModel> MyGroups { get; set; }
        public ObservableCollection<GroupModel> MyGroups_Upload { get; set; }
        public ObservableCollection<GroupModel> MyGroups_Filter { get; set; }
        public GroupModel SelectedGroupModel { get; set; }
        public MySpaceViewModel()
        {
            //GetGroupList();
            spaceUseInfo();
            if (IsInDesignMode)
            {
                //MyGroups = new ObservableCollection<GroupModel>()
                //{
                //  new GroupModel(){GroupName="教材文件夹1"},
                //  new GroupModel(){ GroupName="视频素材" },
                //  new GroupModel(){GroupName="教材文件夹1"},
                //};
            }
            else
            {

            }

            //MyGroups = new ObservableCollection<GroupModel>()
            //    {
            //      new GroupModel(){GroupName="教材文件夹1"},
            //      new GroupModel(){ GroupName="视频素材" },
            //      new GroupModel(){GroupName="教材文件夹1"},
            //    };
        }

        public void GetGroupList(int pages)
        {
            MyGroups = new ObservableCollection<GroupModel>();
            MyGroups_Filter = new ObservableCollection<GroupModel>();
            MyGroups_Upload = new ObservableCollection<GroupModel>();
            MyGroups_Upload.Add(new GroupModel { GroupName = "请选择", });
            MyGroups_Filter.Add(new GroupModel { GroupName = "全部" });
            ReqPersonMessageModel pamrm = new ReqPersonMessageModel
            {
                page = pages,
                limit = 10,
                token = HuiZeeducation.Instance.UserInfo.token,

            };
            string data = JsonHelper.ToJson(pamrm);
            var result = Request<ReqGroupMainModel2>("/api/user_space/groupList", data, "空间分组列表");
            if (result.Item1)
            {
                pageCount2 = result.Item2.data.list.last_page;
                var Groups = result.Item2.data.list.data;
                if (Groups != null)
                {
                    foreach (var group in Groups)
                    {
                        var studentModel =
                        new GroupModel()
                        {
                            GroupName = group.name,
                            Id = group.id,
                        };
                        MyGroups.Add(studentModel);
                        MyGroups_Filter.Add(studentModel);
                        MyGroups_Upload.Add(studentModel);
                    }
                }
            }
        }

        public void addGroup()
        {

            //MyGroups.Clear();
            ReqAddGroupModel pamrm = new ReqAddGroupModel
            {
                name = NewGroupName,
                token = HuiZeeducation.Instance.UserInfo.token,
            };
            string data = JsonHelper.ToJson(pamrm);
            var result = Request<ReqGroupMainModel>("/api/user_space/addGroup", data, "添加分组");
            if (result.Item1)
            {
                ShowDialogView("添加成功!");
                GetGroupList(1);
                groupView.UpdataPageManage();
            }
            else
            {

            }
        }

        public bool delFile(int Id)
        {
            ReqDelFileModel pamrm = new ReqDelFileModel
            {
                id = Id,
                token = HuiZeeducation.Instance.UserInfo.token,
            };
            string data = JsonHelper.ToJson(pamrm);
            var result = Request<ReqDelFileModel>("/api/user_space/delFile", data, "删除文件");
            if (result.Item1)
            {
                spaceUseInfo();
                ShowDialogView("删除成功!");
                return true;
            }
            else
            {
                ShowDialogView("删除失败!");
            }
            return false;
        }

        public void delGroup(int Id)
        {


            ReqDelGroupModel pamrm = new ReqDelGroupModel
            {
                id = Id,
                token = HuiZeeducation.Instance.UserInfo.token,

            };
            string data = JsonHelper.ToJson(pamrm);


            var result = Request<ReqGroupMainModel>("/api/user_space/delGroup", data, "删除分组");
            if (result.Item1)
            {
                ShowDialogView("删除成功!");
                GetGroupList(1);
                groupView.UpdataPageManage();
            }
            else
            {

            }
        }

        public bool editGroup(int Id, string name)
        {
            ReqAlterGroupModel pamrm = new ReqAlterGroupModel
            {
                id = Id,
                token = HuiZeeducation.Instance.UserInfo.token,
                name = name,
            };

            string data = JsonHelper.ToJson(pamrm);
            var result = Request<ReqGroupMainModel>("/api/user_space/editGroup", data, "修改分组");
            if (result.Item1)
            {
                ShowDialogView("修改成功!");
                GetGroupList(1);
                groupView.UpdataPageManage();
                return true;
            }
            else
            {
                ShowDialogView(result.Item2.msg);
            }
            return false;
        }

        public bool editFileGroup(int Id, int _group_id)
        {
            ReqEditFileGroupModel pamrm = new ReqEditFileGroupModel
            {
                id = Id,
                group_id = _group_id,
                token = HuiZeeducation.Instance.UserInfo.token,
            };

            string data = JsonHelper.ToJson(pamrm);
            var result = Request<ReqEditFileGroupModel>("/api/user_space/transgroup", data, "修改分组");
            if (result.Item1)
            {
                ShowDialogView("修改成功!");
                return true;
            }
            else
            {
                ShowDialogView(result.Item2.msg);
            }
            return false;
        }

        public bool uploadFile()
        {
            string filepath = "";
            string filename = "";
            string filesize = "";
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件";
            dialog.Filter = "*|*";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                filepath = dialog.FileName;
                filename = dialog.SafeFileName;
                FileInfo fileInfo = new FileInfo(dialog.FileName);
                filesize = (fileInfo.Length / 1024).ToString();
            }

            if (string.IsNullOrWhiteSpace(filename))
            {
                return false;
            }

            ReqUploadModel model = new ReqUploadModel();

            model.token = HuiZeeducation.Instance.UserInfo.token;
            //model.file = filepath;

            string data2 = JsonHelper.ToJson(model);
            var result2 = Upload<ResUploadModel>("/api/common/oss_upload", data2, "上传文件", filepath, (total, value) =>
             {
                 upload_size_total = total;
                 upload_size_value = value;
             });
            upload_size_total = 1;
            upload_size_value = 0;
            string attachment_id = "";
            if (result2.Item1)
            {
                attachment_id = result2.Item2.data.attachment_id;
            }
            else
            {
                ShowDialogView(result2.Item2.msg);
                return false;
            }

            ReqGroupUserUploadModel2 pamrm = new ReqGroupUserUploadModel2
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                attachment_id = attachment_id,
                group_id = (SelectedUploadGroup != null && SelectedUploadGroup.GroupName != "请选择") ? SelectedUploadGroup.Id : 0,
            };
            string data = JsonHelper.ToJson(pamrm);

            //ReqGroupUploadFileModel pamrm = new ReqGroupUploadFileModel
            //{

            //    token = HuiZeeducation.Instance.UserInfo.token,
            //    file_size = filesize,
            //    group_id=0,
            //    ext_name = filename.Substring(filename.IndexOf('.')+1, filename.Length- filename.IndexOf('.')-1),
            //    file = filepath,
            //    file_name= filename,
            //};
            //string data = JsonHelper.ToJson(pamrm);

            var result = Request<ReqGroupMainModel>("/api/user_space/updSpaceFile", data, "我的空间上传");
            if (result.Item1)
            {
                spaceUseInfo();
                ShowDialogView("上传成功!");
                return true;
            }
            return false;
        }

        public bool uploadFile_background()
        {
            string filepath = "";
            string filename = "";
            string filesize = "";
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件";
            dialog.Filter = "*|*";
            App.Current.Dispatcher.Invoke(new Action(() =>
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    filepath = dialog.FileName;
                    filename = dialog.SafeFileName;
                    FileInfo fileInfo = new FileInfo(dialog.FileName);
                    filesize = (fileInfo.Length / 1024).ToString();
                    upload_file_name = fileInfo.Name;
                }
            }));

            if (string.IsNullOrWhiteSpace(filename))
            {
                return false;
            }

            ReqUploadModel model = new ReqUploadModel();
            model.token = HuiZeeducation.Instance.UserInfo.token;
            //model.file = filepath;

            string data2 = JsonHelper.ToJson(model);
            //var result2 = Upload<ResUploadModel>("/api/common/oss_upload", data2, "上传文件", filepath,(total, value) =>
            //{
            //    upload_size_total = total;
            //    upload_size_value = value;
            //},
            //()=> 
            //{
            //    v_upload_mask = Visibility.Visible;
            //},
            //()=>
            //{
            //    v_upload_mask = Visibility.Collapsed;
            //});
            //upload_size_total = 1;
            //upload_size_value = 0;
            ////string attachment_id = "";
            //if (result2.Item1)
            //{
            //    current_attachment_id = result2.Item2.data.attachment_id;
            //    return true;
            //}
            //else
            //{
            //    App.Current.Dispatcher.Invoke(new Action(() =>
            //    {
            //        ShowDialogView(result2.Item2.msg);
            //    }));
            //}
            //ReqGroupUserUploadModel2 pamrm = new ReqGroupUserUploadModel2
            //{
            //    token = HuiZeeducation.Instance.UserInfo.token,
            //    attachment_id = attachment_id,
            //    group_id = SelectedUploadGroup == null ? 0 : SelectedUploadGroup.Id,
            //};
            //string data = JsonHelper.ToJson(pamrm);
            //var result = Request<ReqGroupMainModel>("/api/user_space/updSpaceFile", data, "我的空间上传");
            //if (result.Item1)
            //{
            //    spaceUseInfo();
            //    current_upload_id = string.IsNullOrEmpty(result.Item2.data.id) ? 0 : Convert.ToInt32(result.Item2.data.id);
            //    App.Current.Dispatcher.Invoke(new Action(() =>
            //    {
            //        ShowDialogView("上传成功!");
            //    }));
            //    return true;
            //}
            return false;
        }

        public bool uploadFile_Complete()
        {
            ReqGroupUserUploadModel2 pamrm = new ReqGroupUserUploadModel2
            {
                token = HuiZeeducation.Instance.UserInfo.token,
                attachment_id = current_attachment_id,
                group_id = (SelectedUploadGroup != null && SelectedUploadGroup.GroupName != "请选择") ? SelectedUploadGroup.Id : 0,
            };
            string data = JsonHelper.ToJson(pamrm);
            var result = Request<ReqGroupMainModel>("/api/user_space/updSpaceFile", data, "我的空间上传");
            if (result.Item1)
            {
                spaceUseInfo();
                current_upload_id = string.IsNullOrEmpty(result.Item2.data.id) ? 0 : Convert.ToInt32(result.Item2.data.id);
                //App.Current.Dispatcher.Invoke(new Action(() =>
                //{
                //    ShowDialogView("上传成功!");
                //}));
                return true;
            }
            return false;
        }

        public void spaceUseInfo()
        {
            ReqGroupSpaceUseModel pamrm = new ReqGroupSpaceUseModel
            {

                token = HuiZeeducation.Instance.UserInfo.token,

            };
            string data = JsonHelper.ToJson(pamrm);
            var result = Request<ReqGroupUseDetailModel>("/api/user_space/spaceUseInfo", data, "空间使用情况");
            if (result.Item1)
            {
                if (result.Item2.data != null)
                {
                    size_total = result.Item2.data.all;
                    size_used = Convert.ToDouble(result.Item2.data.use);

                    useProgress2 = Convert.ToDouble(result.Item2.data.use) * 100;
                    useProgress = Convert.ToInt32(Convert.ToDouble(result.Item2.data.use) * 10);
                }
            }
            else
            {

            }
        }

        public List<ReqGroupFileListDetialModel> spaceFileList(int pageIndex)
        {
            ReqGroupSpaceFileModel pamrm = new ReqGroupSpaceFileModel
            {
                page = pageIndex,
                limit = 21,
                search = serachName,
                group_id = SelectedGroups == null ? 0 : SelectedGroups.Id,
                token = HuiZeeducation.Instance.UserInfo.token,
            };
            string data = JsonHelper.ToJson(pamrm);
            var result = Request<ReqGroupFileListModel>("/api/user_space/spaceFileList", data, "空间文件列表");
            if (result.Item1)
            {
                pageCount = result.Item2.data.last_page;
                return result.Item2.data.data;
            }
            else
            {
                return null;
            }
        }
    }
}
